package cn.dglydrpy.fundmanagement.controller;

import cn.dglydrpy.fundmanagement.service.IPortfolioService;
import cn.dglydrpy.fundmanagement.service.dto.PortfolioDto;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/portfolios")
public class PortfolioController {

    @Autowired
    private IPortfolioService portfolioService;

    @PostMapping("/addPortfolio")
    public void addPortfolio(@RequestBody PortfolioDto portfolioDto) {
        portfolioService.savePortfolio(portfolioDto);
    }

    @PostMapping("/updatePortfolio")
    public void updatePortfolio(@RequestBody PortfolioDto portfolioDto) {
        portfolioService.updatePortfolio(portfolioDto);
    }

    @PostMapping("/deletePortfolio/{id}")
    public void deletePortfolio(@PathVariable Long id) {
        portfolioService.deletePortfolio(id);
    }

    @GetMapping("/{id}")
    public PortfolioDto getPortfolioById(@PathVariable Long id) {
        return portfolioService.getPortfolioById(id);
    }

    @GetMapping("/getPortfoliosByConditions")
    public Page<PortfolioDto> getPortfoliosByConditions(
            @RequestParam String portfolioName,
            @RequestParam int pageNo,
            @RequestParam int pageSize) {
        return portfolioService.getPortfoliosByConditions(portfolioName, pageNo, pageSize);
    }

    @GetMapping("/getAllPortfolios")
    public List<PortfolioDto> getAllPortfolios() {
        return portfolioService.getAllPortfolios();
    }

    @PostMapping("/addFundToPortfolio")
    public void addFundToPortfolio(
            @RequestParam Long portfolioId,
            @RequestParam String fundCode,
            @RequestParam String shares,
            @RequestParam String buyPrice,
            @RequestParam String buyTime) {
        portfolioService.addFundToPortfolio(portfolioId, fundCode, shares, buyPrice, buyTime);
    }

    @PostMapping("/removeFundFromPortfolio")
    public void removeFundFromPortfolio(
            @RequestParam Long portfolioId,
            @RequestParam Long fundId) {
        portfolioService.removeFundFromPortfolio(portfolioId, fundId);
    }
}